class Solution:
    def prevPermOpt1(self, A: list) -> list:
        if len(A) < 2:
            return A

        left = len(A) - 2
        while left >= 0 and A[left + 1] >= A[left]:
            left -= 1
        if left < 0:
            return A

        right = len(A) - 1
        while A[right] >= A[left]:
            right -= 1

        while A[right] == A[right - 1]:
            right -= 1

        temp = A[left]
        A[left] = A[right]
        A[right] = temp

        return A
